<template>
    <div style="
        width: 100vw; 
        height: 100vh; 
        background-image: url('@/assets/images/sea-bg.jpg'); 
        background-size: cover; 
        background-position: center; 
        display: flex; 
        align-items: center; 
        justify-content: center;">
      <div style="width: 450px; padding: 45px; background-color: rgba(255, 255, 255, 0.8); border-radius: 8px; backdrop-filter: blur(5px);">
          <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="登录账号" prop="UserName">
      <el-input v-model="ruleForm.UserName" />
    </el-form-item>
    <el-form-item label="登录密码" prop="Password">
      <el-input type="password" v-model="ruleForm.Password" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
    </div>
    </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import http from '../lanjieqi/lan1'
import axios from '../lanjieqi/lan'
import router from '@/router'
import { lo } from 'element-plus/es/locales.mjs'
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  UserName: '',
  Password: '',

})
const rules = reactive<FormRules>({
  UserName: [
    { required: true, message: '请输入登录账号', trigger: 'blur' },
    { min: 2, max: 15, message: '登录账号为2-15个字段', trigger: 'blur' },
  ],
  Password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change',
    },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) 
    {
      axios.get("api/RoleMenu/Login",{params:
        {
          UserName:ruleForm.UserName,
          Password:ruleForm.Password
        }}).then(res=>
      {
          if(res.data.code==200)
          {
            ElMessage.success(res.data.message)
            localStorage.setItem('id',res.data.data.id)
            localStorage.setItem('roleId',res.data.data.roleId)
            localStorage.setItem('roleName',res.data.data.roleName)
            localStorage.setItem('userName',res.data.data.userName)
            localStorage.setItem('managerName',res.data.data.managerName)
            localStorage.setItem('departmentId',res.data.data.departmentId)
            localStorage.setItem('departmentIds',res.data.data.departmentIds)
            localStorage.setItem('token',res.data.data.token)
            router.push('/main')
          }
          else
          {
            ElMessage.error(res.data.message)
          }
      })
    } 
    else 
    {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>